﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
using XbsWebAPI.Application.CampRoom.Dtos;
using XbsWebAPI.Application.QueryModel;
using XbsWebAPI.Core.Repositories;
using XbsWebAPI.EntityFrameworkCore.Entities;

namespace XbsWebAPI.Application
{
    /// <summary>
    /// 配套设置 APP接口
    /// </summary>
    public class SupportingFacilityAppService : ISupportingFacilityAppService
    {
        private readonly ISupportingFacilityRepository _supportingFacilityRepository;
        private readonly IMapper _mapper;

        public SupportingFacilityAppService(ISupportingFacilityRepository supportingFacilityRepository, IMapper mapper)
        {
            _supportingFacilityRepository = supportingFacilityRepository;
            _mapper = mapper;
        }


        public Task<ResultDto<string>> CreateSupportingFacilityAsync(CreateSupportingFacilityDto model)
        {
             var result = new ResultDto<string>();
            try
            {
                var entity = _mapper.Map<SupportingFacility>(model);
                _supportingFacilityRepository.CreateAsync(entity);
                result.Code = 0;
                result.Message = "操作成功";
                result.Result = "";
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                result.Result = "操作失败";
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }

        public ResultDto<List<ListSupportingFacility>> GetSupportingFacilityList()
        {
            var result = new ResultDto<List<ListSupportingFacility>>();
            try
            {
                var supportingFacilities = _supportingFacilityRepository.AsQueryable().ToList();
                var dtos = supportingFacilities.Select(e => _mapper.Map<ListSupportingFacility>(e)).ToList();
                result.Message = "操作成功";
                result.Result = dtos;
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                result.Result = null;
                return result;
            }

            return result;
        }

        public ResultDto<PageListDto<ListSupportingFacility>> GetSupportingFacilityPagedList(QuerySupportingFacility query)
        {
            var result = new ResultDto<PageListDto<ListSupportingFacility>>();

            try
            {
                var supportingFacilities = _supportingFacilityRepository.AsQueryable();

                if (!string.IsNullOrEmpty(query.Name))
                {
                    supportingFacilities = supportingFacilities.Where(a => a.Name.Contains(query.Name));
                }

                supportingFacilities = supportingFacilities.OrderByDescending(a => a.CreationDate);
                var pagedList = supportingFacilities.ToPagedList(query.PageNumber ?? 1, query.PageSize ?? 10);

                PageListDto<ListSupportingFacility> pagedDto = new PageListDto<ListSupportingFacility>()
                {
                    PageNumber = pagedList.PageNumber,
                    PageSize = pagedList.PageSize,
                    HasPreviousPage = pagedList.HasPreviousPage,
                    HasNextPage = pagedList.HasNextPage,
                    TotalCount = pagedList.TotalItemCount
                };
                var dtos = pagedList.Select(e => _mapper.Map<ListSupportingFacility>(e)).ToList();
                pagedDto.Dtos = dtos;

                result.Code = 0;
                result.Result = pagedDto;
                result.Message = "";
            }
            catch (Exception e)
            {
                result.Code = 1;
                result.Message = e.Message;
                result.Result = null;
                return result;
            }

            return result;
        }

        public Task<ResultDto<string>> RemoveSupportingFacilityAsync(Guid id)
        {
            var result = new ResultDto<string>();

            try
            {
                _supportingFacilityRepository.DeleteAsync(id);

                result.Code = 0;
                result.Result = "操作成功";
                result.Message = "";
            }
            catch (Exception e)
            {
                result.Code = 1;
                result.Message = e.Message;
                result.Result = "操作失败";
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }

        public Task<ResultDto<string>> UpdateSupportingFacilityAsync(EditSupportingFacilityDto model)
        {
            var result = new ResultDto<string>();
            try
            {
                var entity = _mapper.Map<SupportingFacility>(model);
                _supportingFacilityRepository.Update(entity);
                result.Code = 0;
                result.Result = "操作成功";
                result.Message = "";
            }
            catch (Exception e)
            {
                result.Code = 1;
                result.Message = e.Message;
                result.Result = "操作失败";
                return Task.FromResult(result);
            }
            return Task.FromResult(result);
        }
    }
}
